<template>
	<view class="result">
		<view class="top" v-if="video_url">
			<video :src="video_url" mode="aspectFill" class="imuban" autoplay="true" loop="true"
				show-fullscreen-btn="false" enable-progress-gesture="false" show-mute-btn="true"
				play-btn-position="center" id="myVideo1"></video>
		</view>
		<progress :percent="progress" show-info stroke-width="10" class="progress" v-if="progress !=0"  border-radius="10rpx"/>
		<text v-if="tip && tip != 'undefined'"  class="t_tip">{{tip}}</text>
		<button class="btn" @click="saveVideoToAlbum(video_url)" :loading="loadding" :disabled="loadding">保存</button>
		<view class="" style="margin-top: 20rpx; width: 667rpx;" v-if="result_msg && result_msg.startsWith('http')">
			<text class="t_tip">如果保存异常，可以复制链接到浏览器进行下载，该链接30分钟内有效</text>
			<text class="copy_link" @click="onCopyClick">复制链接</text>
			<text class="t_tip">，也可以联系客服免费帮助下载</text>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				video_url: null,
				result_msg: null,
				tip: null,
				loadding: false,
				downloadTask: null,
				progress: 0
			}
		},
		onLoad(options) {
			// #ifdef APP
			this.video_url = options.video_url
			// #endif
			// #ifndef APP
				this.video_url = decodeURIComponent(options.video_url)
			// #endif

			this.tip = options.tip
			this.result_msg = decodeURIComponent(options.result_msg)
		},
		onShow() {
			// 页面显示时触发，继续播放视频
			const videoContext = uni.createVideoContext('myVideo1', this);
			videoContext.play();
		},
		onHide() {
			// 页面隐藏时触发，暂停视频播放
			const videoContext = uni.createVideoContext('myVideo1', this);
			videoContext.pause();
		},
		methods: {
			init_downloadTask() {
				if (!this.downloadTask) return;
				this.downloadTask.onProgressUpdate((res) => {
					this.progress = res.progress
				});
			},
			onCopyClick() {
				let _this = this
				uni.setClipboardData({
					data: this.result_msg,
					showToast: false,
					success: function() {
						_this.$showToast('复制成功')
					}
				});
			},
			saveVideoToAlbum(videoUrl) {
				this.loadding = true
				let _this = this
				this.downloadTask = uni.downloadFile({
					url: videoUrl,
					timeout:600000,
					success: (downloadResult) => {
						if (downloadResult.statusCode === 200) {
							// 将下载的图片保存到相册
							uni.saveVideoToPhotosAlbum({
								filePath: downloadResult.tempFilePath,
								success: () => {
									uni.showToast({
										title: '保存成功'
									});
								},
								fail: (err) => {
									if (err.errMsg == "saveVideoToPhotosAlbum:fail cancel") return
									// 权限问题或其他原因导致保存失败
									console.error('saveImageToPhotosAlbum fail', err);
									uni.showModal({
										title: '保存失败',
										showCancel: false,
										content: err.errMsg,
									});
								}
							});
						} else {
							// 响应的HTTP状态码不是200
							uni.showToast({
								icon: 'none',
								title: '下载失败，HTTP状态码：' + downloadResult.statusCode
							});
						}
					},
					fail: (e) => {
						if (!e) return
						uni.showModal({
							content: e.errMsg || e.message,
							showCancel: false
						});
					},
					complete: () => {
						_this.loadding = false
					}
				});
				this.init_downloadTask()
			}
			
		}
	}
</script>

<style lang="scss">
	@import "VideoResult.scss";
</style>